package controller

import (
	"net/http"
	"strconv"

	"gitee.com/ysl729895/go-mall/internal/model"
	"gitee.com/ysl729895/go-mall/internal/service"
	"github.com/gin-gonic/gin"
)

// UserRoleController 用户-角色分配控制器，处理用户分配/移除角色、查询用户角色等 HTTP 请求

type UserRoleController struct {
	service service.UserRoleService
}

// NewUserRoleController 构造函数，注入 UserRoleService
func NewUserRoleController(service service.UserRoleService) *UserRoleController {
	return &UserRoleController{service: service}
}

// AssignRoleToUser 给用户分配角色
// @Summary 给用户分配角色
// @Tags 用户角色分配
// @Accept json
// @Produce json
// @Param user_id path int true "用户ID"
// @Param role_id body model.UserRole true "角色ID"
// @Success 200 {string} string "分配成功"
// @Failure 400 {object} model.ErrorResponse
// @Failure 500 {object} model.ErrorResponse
// @Router /api/users/{user_id}/roles [post]
func (c *UserRoleController) AssignRoleToUser(ctx *gin.Context) {
	userIDStr := ctx.Param("user_id")
	userID, err := strconv.Atoi(userIDStr)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, model.ErrorResponse{
			Code:    http.StatusBadRequest,
			Message: "无效的用户ID",
			Error:   err.Error(),
		})
		return
	}
	var req struct {
		RoleID uint `json:"role_id" binding:"required"`
	}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, model.ErrorResponse{
			Code:    http.StatusBadRequest,
			Message: "请求参数格式不正确",
			Error:   err.Error(),
		})
		return
	}
	if err := c.service.AssignRoleToUser(uint(userID), req.RoleID); err != nil {
		ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.ErrorResponse{
			Code:    http.StatusInternalServerError,
			Message: "分配角色失败",
			Error:   err.Error(),
		})
		return
	}
	ctx.Set("responseData", gin.H{"user_id": userID, "role_id": req.RoleID})
	ctx.Set("responseMessage", "分配角色成功")
}

// RemoveRolesFromUser 移除用户所有角色
// @Summary 移除用户所有角色
// @Tags 用户角色分配
// @Param user_id path int true "用户ID"
// @Success 200 {string} string "移除成功"
// @Router /api/users/{user_id}/roles [delete]
func (c *UserRoleController) RemoveRolesFromUser(ctx *gin.Context) {
	userIDStr := ctx.Param("user_id")
	userID, err := strconv.Atoi(userIDStr)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, model.ErrorResponse{
			Code:    http.StatusBadRequest,
			Message: "无效的用户ID",
			Error:   err.Error(),
		})
		return
	}
	if err := c.service.RemoveRolesFromUser(uint(userID)); err != nil {
		ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.ErrorResponse{
			Code:    http.StatusInternalServerError,
			Message: "移除角色失败",
			Error:   err.Error(),
		})
		return
	}
	ctx.Set("responseData", gin.H{"user_id": userID})
	ctx.Set("responseMessage", "移除角色成功")
}

// GetRolesByUserID 查询用户所有角色
// @Summary 查询用户所有角色
// @Tags 用户角色分配
// @Param user_id path int true "用户ID"
// @Success 200 {array} model.Role
// @Router /api/users/{user_id}/roles [get]
func (c *UserRoleController) GetRolesByUserID(ctx *gin.Context) {
	userIDStr := ctx.Param("user_id")
	userID, err := strconv.Atoi(userIDStr)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, model.ErrorResponse{
			Code:    http.StatusBadRequest,
			Message: "无效的用户ID",
			Error:   err.Error(),
		})
		return
	}
	roles, err := c.service.GetRolesByUserID(uint(userID))
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.ErrorResponse{
			Code:    http.StatusInternalServerError,
			Message: "查询用户角色失败",
			Error:   err.Error(),
		})
		return
	}
	ctx.Set("responseData", roles)
}
